package edu.cuit.zhuyimeng.ticket.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.dynamic.datasource.annotation.Slave;
import edu.cuit.zhuyimeng.flight.api.controller.FlightQueryApiController;
import edu.cuit.zhuyimeng.flight.api.dto.FlightInfoDTO;
import edu.cuit.zhuyimeng.framework.cache.aspect.annotation.CacheInvalidatePattern;
import edu.cuit.zhuyimeng.framework.common.exception.QueryException;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.ticket.controller.vo.TicketInfoVO;
import edu.cuit.zhuyimeng.ticket.enums.CacheConstants;
import edu.cuit.zhuyimeng.ticket.service.IFlightService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 航班相关服务接口
 */
@Service
@RequiredArgsConstructor
public class FlightService implements IFlightService {

    private final FlightQueryApiController flightQueryApiController;

    private final Cache<String, TicketInfoVO> ticketInfoCache;
    private final Cache<Long, FlightInfoDTO> flightInfoCache;

    @Override
    @Cached(name = CacheConstants.FLIGHT_INFO,key = "args[0]")
    public FlightInfoDTO queryFlightInfo(Long flightId) {
        CommonResult<FlightInfoDTO> flightInfoResult = flightQueryApiController.queryOneFlight(flightId);
        if (flightInfoResult.isError()) {
            throw new QueryException(flightInfoResult.getMsg());
        }
        return flightInfoResult.getData();
    }

    @Override
    @CacheInvalidatePattern(name = CacheConstants.TICKET_INFO,pattern = "*")
    public void refreshFlightCache(Long flightId) {
        flightInfoCache.remove(flightId);
    }

}
